• 电话咨询

  • 13592986386
所在位置:网页微信授权开发

网页微信授权开发

阅读:677 发表时间:2018/5/15 15:11:31






Bin 引用DLL

AppCode引用类文件




 protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            string userAgent = Request.UserAgent;
            if (userAgent.ToLower().Contains("micromessenger"))
            {
                //Response.Write("欢迎您在微信中访问我。"); 
                //code=CODE&state=STATE
                if (Request.QueryString["code"] != null)
                {
                    //第一步:定义APPID等
                    string APPID = "*************************";
                    string APPSECRET = "*************************"; ;

                    //第二步:通过code换取网页授权access_token
                    string code = Request.QueryString["code"];

                    ZSJson myjson = new ZSJson();
                    string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + APPSECRET + "&code=" + code + "&grant_type=authorization_code";
                    string strReturn = myjson.GetFunction(url);
                    JObject jo = JObject.Parse(strReturn);
                    string ACCESS_TOKEN = jo["access_token"].ToString();    //读取获取到的ACCESS_TOKEN
                    string OPENID = jo["openid"].ToString();                //读取获取到的OPENID
                    Session["openid"] = OPENID;

                    //第四步:拉取用户信息(需scope为 snsapi_userinfo)
                    string nexturl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + ACCESS_TOKEN + "&openid=" + OPENID + "&lang=zh_CN";
                    string nextstrReturn = myjson.GetFunction(nexturl);
                    //Response.Write(nextstrReturn);
                    JObject result = JObject.Parse(nextstrReturn);
                    string nickname = result["nickname"].ToString();
                    string sex = result["sex"].ToString();
                    string headimgurl = result["headimgurl"].ToString();
                    string province = result["province"].ToString();
                    string city = result["city"].ToString();


                    /************业务逻辑(默认注册帐号,注册完成后进入某个指定页面)****************************/
                    string Password = "123456";                           //密码
                    string cMD5Pass = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");  //MD5加密
                    SqlConn mysql = new SqlConn();
                    Hashtable httParam = new Hashtable();
                    httParam.Add("@OPType", 777);
                    httParam.Add("@iRole", 1);
                    httParam.Add("@OPENID", OPENID);
                    httParam.Add("@cUserName", nickname);
                    httParam.Add("@cMobile", "");
                    httParam.Add("@cPic", headimgurl);
                    httParam.Add("@cPassword", cMD5Pass);
                    string StoredtName = "tb_User_Proc";
                    DataTable dt = mysql.ExecuteDataTable(StoredtName, httParam, CommandType.StoredProcedure);
                    if (dt.Rows.Count > 0)
                    {
                        CookieManage mycookies = new CookieManage();
                        mycookies.DeleteCookie("UserID");
                        mycookies.DeleteCookie("UserName");
                        mycookies.DeleteCookie("UserMobile");

                        int cookiestime = 1440;
                        mycookies.WriteCookie("UserID", dt.Rows[0]["ID"].ToString(), cookiestime, true);
                        mycookies.WriteCookie("UserName", dt.Rows[0]["cUserName"].ToString(), cookiestime, true);
                        mycookies.WriteCookie("UserMobile", dt.Rows[0]["cMobile"].ToString(), cookiestime, true);

                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language=javascript>autoPage()</script>");
                    }
                    /*************************************/
                }
                else
                {
                    //第一步:用户同意授权,获取code
                    //在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:
                    string APPID = "*************************";
                    string webdomain = "http://" + HttpContext.Current.Request.Url.Host;
                    string REDIRECT_URI = webdomain + "/Default.aspx";
                    string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPID + "&redirect_uri=" + REDIRECT_URI + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                    Response.Redirect(url);
                }
            }
            else
            {
                //非微信浏览器打开(电脑,手机浏览器)
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language=javascript>autoPage()</script>");
            }
        }
    }

致胜软件

 

因为通用软件的局限性而改变企业经营方式?

那这是反人类的!!!

致胜软件根据需求定制,永远为您而变!

 

致胜网络的理念

 

面对面给客户最实在的 最贴心的服务!

强大的技术团队,过百的案例与经验!

在三线城市开发,按三线城市等级收费!

 

致胜网络介绍

 

2009年成立,至今7年。!

7年的专业,7年之痒!

期待可以为您提供长期服务!

 

想进一步了解

 

欢迎您的咨询?

QQ:1990654

微信:13592986386

电话:13592986386

网友留言
我也来评论
友情连接: 茂名传媒网   
茂名网站建设,茂名网站设计,茂名APP开发,茂名软件开发,茂名OA办公系统---致胜网络
联系電話:13592986386  Email:1990654@qq.com
公司地址:茂名市人民南路新村大院22号101
联系  QQ:1990654   1990654   
备案号码:粤ICP备11102194号-4
登陆后台 技术支持:致胜网络